package com.hbk.controller;

import com.alibaba.fastjson.JSONObject;
import com.hbk.base.common.CommonResult;
import com.hbk.base.common.Ref;
import com.hbk.base.component.EnvPostProcessor;
import com.hbk.base.component.ExternalConfig;
import com.hbk.base.util.StringUtil;
import com.hbk.param.NewsParam;
import com.hbk.service.INewsService;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/api/news")
public class NewsController {

    @Resource
    private INewsService newsService;

    private ExternalConfig config = EnvPostProcessor.getExternalConfig();

    /**后台分页查询新闻列表*/
    @RequestMapping("/list")
    public CommonResult list(@Valid NewsParam.ListParam param){
        return newsService.list(param);
    }

    /**新闻封面图片上传*/
    @RequestMapping("/addImg")
    public CommonResult addImg(HttpServletRequest request){

        if(!(request instanceof MultipartHttpServletRequest)){
            System.out.println("不是文件上传");
            return Ref.error("不是文件上传");
        }
        MultipartHttpServletRequest req = (MultipartHttpServletRequest) request;
        MultiValueMap<String, MultipartFile> multiFileMap = req.getMultiFileMap();
        if(multiFileMap==null || multiFileMap.size()==0){
            return Ref.error("上传文件为空");
        }
        MultipartFile file = multiFileMap.getFirst("file");
        if(file==null){
            return Ref.error("上传文件为空");
        }
        String originalFilename = file.getOriginalFilename();
        System.out.println(originalFilename);
        int i = originalFilename.lastIndexOf(".");
        String fileName = StringUtil.randomName() + originalFilename.substring(i);
        String filePath = config.getEditor().getImgDir() + "/news/" + fileName;
        File imgFile = new File(filePath);
        File parentFile = imgFile.getParentFile();
        if(!parentFile.exists()){
            parentFile.mkdirs();
        }
        try {
            file.transferTo(imgFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String imgUrl = "/upload/news/" + fileName;
        Map<String,String> map = new HashMap<>();
        map.put("id","101");
        map.put("imgUrl",imgUrl);
        map.put("prefix", config.getEditor().getImgPrefix());
        return Ref.ok(map);
    }

    /**新增新闻*/
    @RequestMapping("/add")
    public CommonResult add(@Valid NewsParam.AddParam param){

        return newsService.add(param);
    }

    /**修改新闻*/
    @RequestMapping("/modify")
    public CommonResult modify(@Valid NewsParam.ModifyParam param){
        return newsService.modify(param);
    }


    /**获取新闻详情*/
    @RequestMapping("/get")
    public CommonResult get(@Valid NewsParam.GetParam param){
        return newsService.get(param);
    }

    /**发布新闻*/
    @RequestMapping("/publish")
    public CommonResult publish(@Valid NewsParam.PublishParam param){
        return newsService.publish(param);
    }

    /**取消发布*/
    @RequestMapping("/cancel")
    public CommonResult cancel(@Valid NewsParam.CancelParam param){
        return newsService.cancel(param);
    }

    /**删除新闻*/
    @RequestMapping("/delete")
    public CommonResult delete(@Valid NewsParam.DeleteParam param){
        return newsService.delete(param);
    }

    /**修改序号*/
    @RequestMapping("/commandOrderNum")
    public CommonResult commandOrderNum(NewsParam.CommandOrderNumParam param){
        return newsService.commandOrderNum(param);
    }

    /**前端获取新闻列表*/
    @RequestMapping("/webList")
    public CommonResult webList(@Valid NewsParam.WebListParam param){
        return newsService.webList(param);
    }



    /**其他新闻列表*/
    @RequestMapping("/otherList")
    public CommonResult otherList(){
        return newsService.otherList();
    }

    /**获取其他新闻的详情*/
    @RequestMapping("/getOther")
    public CommonResult getOther(NewsParam.GetOtherParam param){
        return newsService.getOther(param);
    }

    /**修改其他新闻的详情*/
    @RequestMapping("/modifyOther")
    public CommonResult modifyOther(String contentId, String content, String attachList){
        NewsParam.ModifyOtherParam param = new NewsParam.ModifyOtherParam();
        param.setContent(content);
        param.setContentId(contentId);
        if(!StringUtil.isEmpty(attachList)){
            List<NewsParam.AttachParam> array = null;
            try{
                array = JSONObject.parseArray(attachList, NewsParam.AttachParam.class);
            }catch (Exception e){
                e.printStackTrace();
            }
            if(array!=null && array.size()>0){
                param.setAttachList(array);
            }
        }
        return newsService.modifyOther(param);
    }


    /**前端获取新闻列表*/
    @RequestMapping("/pageList")
    public CommonResult pageList(@Valid NewsParam.PageListParam param){
        return newsService.pageList(param);
    }
}
